function getUrl(){
    var url = window.location.href;
    var path = window.location.pathname;
    var search = window.location.search;
    url = url.replace(path,"/");
    url = url.replace(search,"")
    return url;
}

$(document).ready(function () {

    const url = getUrl();

    $.ajax({
        url:url+"adminInfo/listCourse",
        data:{},
        type: "get",
        contentType: "application/json",
        success: function(data) {
            if(data=="unLogin"){
                location.href="login.html";
            }else{
                if(!($.isEmptyObject(data))){
                    $.each(data, function (i, course) {
                        console.log(course);

                        const mode =
                            '<div class="row-fluid">'+
                                '<div class="span2">'+
                                    '<div>'+
                                        '<p id="id"></p>'+
                                    '</div>'+
                                '</div>'+
                                '<div class="span3">'+
									'<div>'+
                                        '<input class="input-large disabled" id="name" type="text" placeholder="" disabled="">'+
											
									'</div>'+
								'</div>'+
                                '<div class="span3">'+
									'<div id="type_">'+
                                        '<input class="input-large disabled" id="type" type="text" placeholder="" disabled="">'+
											
									'</div>'+
								'</div>'+
                                '<div class="span1">'+
									'<div>'+
                                        '<input class="input-small disabled" id="credit" type="text" placeholder="" disabled="">'+
											
									'</div>'+
								'</div>'+
                                '<div class="span3" id="course_active">'+
                                    '<div class="span6" id="editCourse_">'+
                                        '<button class="btn btn-small btn-primary" id="editCourse" onclick= "">编辑</button>'+
                                    '</div>'+
                                    '<div class="span6" id="deleteCourse_">'+
                                        '<button class="btn btn-small btn-danger" id="deleteCourse" onclick= "">删除</button>'+
                                    '</div>'+
                                '</div>'+
                            '</div>';

                        const $obj = $(mode);
                        $obj.find("#id").text(course.id);
                        $obj.find("#name").val(course._name);
                        $obj.find("#type").val(course.type);
                        $obj.find("#credit").val(course.credit);

                        $("#course_body").append($obj);

                    })
                }
            }
        },
        error:function(){
            console.log("----------")
        }
    })

    $("#course_body").on('click','#editCourse',function(){
        console.log("changeGrade");
        const id = $(this).parent().parent().parent().find("#id").text();
        console.log(id);

        const t = $(this).parent().parent().parent().find("#type").val();
        console.log(t);

        $(this).parent().parent().parent().find("#name").removeAttr("disabled");
        $(this).parent().parent().parent().find("#credit").removeAttr("disabled");
        $(this).parent().parent().parent().find("#type").remove();

        const save = '<div class="span6" id="confirmEdit_"><button class="btn btn-small btn-danger" id="confirmEdit">确认</button></div class="span6">';
        const cancel = '<div class="span6" id="cancelEdit_"><button class="btn btn-small btn-inverse" id="cancelEdit">取消</button></div class="span6">';
        const type = 
        '<select id="type">'+
            '<option>专业基础课/必修课</option>'+
            '<option>专业课/必修课</option>'+
            '<option>专业课/任选课</option>'+
            '<option>专业课/限选课</option>'+
            '<option>公共课/必修课</option>'+
            '<option>公共课/任选课</option>'+
            '<option>专业基础课/必修课(重修)</option>'+
            '<option>专业课/必修课(重修)</option>'+
            '<option>专业课/任选课(重修)</option>'+
            '<option>专业课/限选课(重修)</option>'+
            '<option>公共课/必修课(重修)</option>'+
            '<option>公共课/任选课(重修)</option>'+
        '</select>	'

        const $mode1 = $(save);
        const $mode2 = $(cancel);
        const $mode3 = $(type);

        $(this).parent().parent().append($mode1);
        $(this).parent().parent().append($mode2);
        $(this).parent().parent().parent().find("#type_").append($mode3);
        $(this).parent().parent().parent().find("#type").val(t);
        $(this).parent().parent().find("#deleteCourse_").remove();
        $(this).parent().parent().find("#editCourse_").remove();
    })

    $("#course_body").on('click','#cancelEdit',function(){
        const t = $(this).parent().parent().parent().find("#type").val();
        console.log(t);
        
        $(this).parent().parent().parent().find("#name").attr('disabled',"");
        $(this).parent().parent().parent().find("#credit").attr('disabled',"");
        $(this).parent().parent().parent().find("#type").remove();

        const save = '<div class="span6" id="changeGrade_"><button class="btn btn-small btn-primary" id="editCourse" onclick= "">编辑</button></div>';
        const cancel = '<div class="span6" id="deleteGrade_"><button class="btn btn-small btn-danger" id="deleteCourse" onclick= "">删除</button></div>';
        const type = '<input class="input-large disabled" id="type" type="text" placeholder="" disabled="">';
        const $mode1 = $(save);
        const $mode2 = $(cancel);
        const $mode3 = $(type);

        $(this).parent().parent().append($mode1);
        $(this).parent().parent().append($mode2);
        $(this).parent().parent().parent().find("#type_").append($mode3);
        $(this).parent().parent().parent().find("#type").val(t);
        $(this).parent().parent().find("#confirmEdit_").remove();
        $(this).parent().parent().find("#cancelEdit_").remove();
    })

    $("#course_body").on('click','#confirmEdit',function(){
        const map ={
            "id":$(this).parent().parent().parent().find("#id").text(),
            "name":$(this).parent().parent().parent().find("#name").val(),
            "credit":$(this).parent().parent().parent().find("#credit").val(),
            "type":$(this).parent().parent().parent().find("#type").val()
        }

        console.log(map);

        $.ajax({
            url:url+"adminInfo/updateCourse",
            data:JSON.stringify(map),
            type: "post",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="courseManage.html";
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
    })

    $("#course_body").on('click','#addCourse',function(){
        const map ={
            "name":$("#name").val(),
            "type":$("#type").val(),
            "credit":$("#credit").val()
        }
    
        console.log(map);
    
    
        $.ajax({
            url:url+"adminInfo/addCourse",
            data:JSON.stringify(map),
            type: "post",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="courseManage.html";
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
    });

    $("#course_body").on('click','#deleteCourse',function(){
        const id = $(this).parent().parent().parent().find("#id").text();
        console.log(id);
        $.ajax({
            url:url+"adminInfo/deleteCourse",
            data:{
                index:id,
            },
            type: "get",
            contentType: "application/json; charset=utf-8",
            success: function(id) {
                console.log(id);
                location.href="courseManage.html";
            },
            error:function(id){
                console.log("----------");
                console.log(id);
            }
        })
    });

});

$("#add_Course").click(function () {
    const mode = 
    '<div class="row-fluid">'+
        '<div class="span2">'+
            '<div>'+
                '<p id="id"></p>'+
            '</div>'+
        '</div>'+
        '<div class="span3">'+
			'<div>'+
                '<input class="input-large disabled" id="name" type="text" placeholder="">'+
											
			'</div>'+
		'</div>'+
        '<div class="span3">'+
			'<div>'+
				'<select id="type">'+
					'<option>专业基础课/必修课</option>'+
					'<option>专业课/必修课</option>'+
                    '<option>专业课/任选课</option>'+
                    '<option>专业课/限选课</option>'+
                    '<option>公共课/必修课</option>'+
                    '<option>公共课/任选课</option>'+
                    '<option>专业基础课/必修课(重修)</option>'+
					'<option>专业课/必修课(重修)</option>'+
                    '<option>专业课/任选课(重修)</option>'+
                    '<option>专业课/限选课(重修)</option>'+
                    '<option>公共课/必修课(重修)</option>'+
                    '<option>公共课/任选课(重修)</option>'+
				'</select>	'+
			'</div>'+
		'</div>'+
        '<div class="span1">'+
			'<div>'+
                '<input class="input-small disabled" id="credit" type="text" placeholder="">'+
											
			'</div>'+
		'</div>'+
        '<div class="span3" id="course_active">'+
            '<button class="btn btn-small btn-primary" id="addCourse" onclick= "">添加</button>'+
        '</div>'+
    '</div>';
                
    const $obj = $(mode);
    $("#course_title").after($obj);
});